<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 支付回调通知
 */
namespace app\smartbc\controller\api\v1;
use app\smartbc\controller\api\Base;
use app\smartbc\model\SmartbcBill;
use app\smartbc\model\SmartbcCouponUser;
use app\smartbc\model\SmartbcOrder;
use app\common\facade\WechatPay;
use app\smartbc\model\SmartbcStoreBill;
use filter\Filter;
use think\facade\Log;
use Exception;

class Notify extends Base{

    /**
     * 微信付款买单
     * @return void
     */
    public function storePay(){
        try {
            $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
                $result = SmartbcOrder::where(['state' => 0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
                if (empty($result)) {
                    return true;
                }
                if ($message['return_code'] === 'SUCCESS') {
                    if ($message['result_code'] === 'SUCCESS') {
                        $sub_mchid = $message['sub_mch_id'] ?? '';
                        $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($result->order_no);
                        if ($ispay['return_code'] === 'SUCCESS') {
                            if ($ispay['result_code'] === 'SUCCESS') {
                                if ($ispay['trade_state'] === 'SUCCESS'){
                                    //订单状态
                                    $result->state     = 1;
                                    $result->paid_time = strtotime($ispay['time_end']);
                                    $result->paid_no   = $ispay['transaction_id'];
                                    //收益计算
                                    $allMoney = money(($ispay['total_fee'] - $ispay['total_fee'] * 6 / 1000) / 100);
                                    $result = SmartbcOrder::income($result,$allMoney);
                                    //消费日志
                                    SmartbcBill::add(['member_miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'money' => -$result->price,'uid' => $result->uid],'微信支付[消费]');
                                    SmartbcStoreBill::add(['member_miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'store_chain_id' => $result->store_chain_id,'money' => $result->real,'pay_uid' => $result->uid],'微信支付[收款]');
                                    if ($result->coupon_user_id > 0) {
                                        SmartbcCouponUser::where(['id' => $result->coupon_user_id])->update(['is_end' => 1,'money' => money($result->amount - $result->price),'update_time' => time()]);    //优惠券减少
                                    }
                                    $result->allowField(true)->save();
                                    return true;
                                }
                            }
                        }
                    }
                }
                return $fail('通信失败,请稍后再通知我');
            });
            $response->send();
        }catch (Exception $e) {
            $this->error('页面不存在');
        }
    }
}